package com.example.demo2.serviceimpl;


import com.example.demo2.mapper.BELONGMapper;
import com.example.demo2.mapper.TEXTMapper;
import com.example.demo2.mapper.TYPEMapper;
import com.example.demo2.mapper.USERMapper;
import com.example.demo2.model.*;
import com.example.demo2.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    TYPEMapper typeMapper;
    @Resource
    BELONGMapper belongMapper;
    @Resource
    TEXTMapper textMapper;
    @Resource
    USERMapper userMapper;
    @Override
    public ResponseBo addType(TYPE type) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addType 方法");
        Date date=new Date();
        type.setCreatetime(date);
        int res=typeMapper.insert(type);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addType 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addType 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getTypes() {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getTypes 方法");
        List list=typeMapper.selectAll();
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getTypes 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getTypes 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getBelongs() {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getBelongs 方法");
        List list=belongMapper.selectAll();
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getBelongs 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getBelongs 方法");
        return responseBo;
    }

    @Override
    public ResponseBo addBelongs(BELONG belong) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addBelongs 方法");
        Date date=new Date();
        belong.setCreatetime(date);
        int res=belongMapper.insert(belong);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addBelongs 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addBelongs 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getTexts(int typeid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getTexts 方法");
        List list=textMapper.selectByTypeid(typeid);
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getTexts 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getTexts 方法");
        return responseBo;
    }

    @Override
    public ResponseBo addTexts(TEXT text) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addTexts 方法");
        Date date=new Date();
        text.setCreatetime(date);
        int res=textMapper.insert(text);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addTexts 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addTexts 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getUserById(int userid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getUserById 方法");
        USER user=userMapper.selectByPrimaryKey(userid);
        if (user==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getUserById 方法");
            return responseBo;
        }
        responseBo.setResult(user);
        responseBo.setResMsg("获取成功");
        log.info("退出 AdminServiceImpl 类的 getUserById 方法");
        return responseBo;
    }
}
